import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import employeesAsyncRoute from './modules/employees'
import departmentsAsyncRoute from './modules/departments'
import settingsAsyncRoute from './modules/settings'
import permissionsAsyncRoute from './modules/permissions'
// 动态路由表
// 路由模块化
const asyncRoutes = [
  departmentsAsyncRoute,
  settingsAsyncRoute,
  employeesAsyncRoute,
  permissionsAsyncRoute

  // 放动态路由对象

  // {
  //   path: '/employees',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '', // 作为默认渲染路由
  //       name: 'employees',
  //       component: () => import('@/views/employees/employees.vue'),
  //       meta: { title: '员工管理', icon: 'people' }
  //     }
  //   ]
  // },
  // {
  //   path: '/departments',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '', // 作为默认渲染路由
  //       name: 'departments',
  //       component: () => import('@/views/departments/departments.vue'),
  //       meta: { title: '组织架构', icon: 'tree' }
  //     }
  //   ]
  // },
  // {
  //   path: '/settings',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '', // 作为默认渲染路由
  //       name: 'settings',
  //       component: () => import('@/views/settings/settings.vue'),
  //       meta: { title: '公司设置', icon: 'tree' }
  //     }
  //   ]
  // },
  // {
  //   path: '/permissions',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '', // 作为默认渲染路由
  //       name: 'permissions',
  //       component: () => import('@/views/permissions/permissions.vue'),
  //       meta: { title: '权限管理', icon: 'tree' }
  //     }
  //   ]
  // }

]

// 静态路由表
export const constantRoutes = [
  {
    path: '/demo',
    component: () => import('@/views/demo/index.vue'),
    hidden: true
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes, ...asyncRoutes] // 最终生成的路由有动态和静态合并在一起
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
